﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Library.Entity;
using Library.Connection;
using System.Data.SqlClient;
using Library.SQL;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;

namespace Library.Services
{
    public class MemberClassService
    {
        DataBaseConnectivity m_dataBaseConectivity = new DataBaseConnectivity();
        public MemberClassService()
        { }
        internal MemberClass GetMemberClassById(int Id)
        {
            MemberClass result = null;
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.SelectMemberClassById(), m_dataBaseConectivity.MySqlConnection);
                cmd.Parameters.Add("Id", MySqlDbType.Int32).Value = Id;
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new MemberClass(rd);
                }
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
            return result;
        }
        internal MemberClass GetMemberClassByMemberClassCode(string MemberClassCode)
        {
            MemberClass result = null;
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.SelectMemberClassByCode(), m_dataBaseConectivity.MySqlConnection);
                cmd.Parameters.Add("Code", MySqlDbType.VarChar).Value = MemberClassCode;
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new MemberClass(rd);
                }
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
            return result;
        }

        internal void InsertLog(MemberClass m_MemberClass, string p)
        {
          
        }

        internal IList GetAllMemberClass()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.SelectAllMemberClass(), m_dataBaseConectivity.MySqlConnection);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new MemberClass(rd));
                }
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
            return result;
        }

        internal void InsertNewMemberClass(MemberClass MemberClass)
        {
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.InsertMemberClass(), m_dataBaseConectivity.MySqlConnection);

                cmd.Parameters.Add("Code", MySqlDbType.VarChar).Value = MemberClass.Code;
                cmd.Parameters.Add("Name", MySqlDbType.VarChar).Value = MemberClass.Name;
                cmd.Parameters.Add("Remark", MySqlDbType.VarChar).Value = MemberClass.Remark;
                cmd.Parameters.Add("CreatedDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("ModifiedDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("CreatedUserId", MySqlDbType.Int32).Value = MemberClass.CreatedUserId;
                cmd.Parameters.Add("ModifiedUserId", MySqlDbType.Int32).Value = MemberClass.ModifiedUserId;
                cmd.ExecuteNonQuery();
                cmd.CommandText = SQLScript.SelectMaxMemberClassId();
                cmd.Parameters.Clear();
                MemberClass.Id = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
        }

        internal void UpdateMemberClass(MemberClass MemberClass)
        {
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.UpdateMemberClass(), m_dataBaseConectivity.MySqlConnection);

                cmd.Parameters.Add("Code", MySqlDbType.VarChar).Value = MemberClass.Code;
                cmd.Parameters.Add("Name", MySqlDbType.VarChar).Value = MemberClass.Name;
                cmd.Parameters.Add("Remark", MySqlDbType.VarChar).Value = MemberClass.Remark;
                cmd.Parameters.Add("ModifiedDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("ModifiedUserId", MySqlDbType.Int32).Value = MemberClass.ModifiedUserId;
                cmd.Parameters.Add("Id", MySqlDbType.Int32).Value = MemberClass.Id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
        }

        internal void DeleteMemberClass(MemberClass MemberClass)
        {
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.DeleteMemberClass(), m_dataBaseConectivity.MySqlConnection);
                cmd.Parameters.Add("Id", MySqlDbType.Int32).Value = MemberClass.Id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
        }
    }
}